package com.baymax.test.scene.demo.maverick

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksView
import com.airbnb.mvrx.MavericksViewInternalViewModel
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.withState
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newSceneFlexContainer
import com.bytedance.scene.ktx.requireNavigationScene

class CounterDemo : BaseScene(), MavericksView {
    lateinit var text: android.widget.Button
    private val viewModel: CounterViewModel by sceneViewModel()
    override fun onCreateView(
        p0: LayoutInflater,
        p1: ViewGroup,
        p2: Bundle?
    ): View = p1.newSceneFlexContainer {

        text = addButton("r:${viewModel.start.count}")

        addButton("increment") {
            viewModel.increment()
        }
        addButton("increment") {
            withState(viewModel) { s ->
                viewModel.decrement()
            }
        }

    }


    override fun invalidate() {
        withState(viewModel) { state ->
            text.text = "r:${state.count}"
        }

    }

    data class CounterState(val count: Int = 0) : MavericksState
    class CounterViewModel(val start: CounterState) : MavericksViewModel<CounterState>(start) {

        init {
            onEach {

            }

        }

        fun increment(): Unit {
            setState {
                println("inc")
                copy(count = count + 1)
            }
        }

        fun decrement(): Unit {
            setState { copy(count = count - 1) }
        }
    }
}